//
//  DateSelectView.m
//  LoginAndRegister
//
//  Created by 张健 on 2021/11/19.
//

#import "DateSelectView.h"
#import <YYKit/YYKit.h>
#import "UIColor+Expanded.h"
#import "MMHeader.h"

@interface  DateSelectView()

@property(nonatomic,strong)TWDatePicker * dataPicker;

@property(nonatomic,strong)UIView * bgView;

@property(nonatomic,strong)UILabel * titleLab;

@property(nonatomic,strong)UIButton * cancle;

@property(nonatomic,strong)UIButton * sure;

@end

@implementation DateSelectView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addUI];
    }
    return self;
}

-(void)addUI{
    
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.titleLab];
    [self.titleLab addSubview:self.cancle];
    [self.titleLab addSubview:self.sure];
    self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT - 318, SCREEN_WIDTH, 318);
    self.titleLab.frame = CGRectMake(0, 0, SCREEN_WIDTH, 34);
    self.cancle.frame = CGRectMake(16, 0, 40, 34);
    self.sure.frame = CGRectMake(SCREEN_WIDTH - 16 - 40, 0, 40, 34);
}

-(void)showWithMode:(TWDatePickerMode)pickerMode AndDate:(NSDate *)selectDate{
//    NSDate * date = [NSDate new];
    self.dataPicker.pickerMode = pickerMode;
    self.dataPicker.date = selectDate;
    self.dataPicker.selectDate = selectDate;
    [self.bgView addSubview:self.dataPicker];
    for (UIWindow * window in [UIApplication sharedApplication].windows) {
        if (window.isKeyWindow) {
            [window addSubview:self];
        }
    }
}

-(void)sureClick{
    if (_blockSelectDone) {
        self.blockSelectDone(self.dataPicker.selectDate);
        [self dismiss];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}

-(void)dismiss{
    [self removeFromSuperview];
}

- (TWDatePicker *)dataPicker{
    if (!_dataPicker) {
        _dataPicker = [[TWDatePicker alloc]initWithFrame:CGRectMake(0, 34, SCREEN_WIDTH, 284)];
    }
    return _dataPicker;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc]init];
        _titleLab.userInteractionEnabled = YES;
        _titleLab.text = @"请选择日期";
        _titleLab.textColor = [UIColor whiteColor];
        _titleLab.font = [UIFont systemFontOfSize:12];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.backgroundColor = [UIColor colorWithHexString:@"#D1AE82"];
    }
    return _titleLab;
}

- (UIButton *)cancle{
    if (!_cancle) {
        _cancle =  [[UIButton alloc]init];
        [_cancle setTitle:@"取消" forState:UIControlStateNormal];
        [_cancle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancle.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_cancle addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancle;
}

- (UIButton *)sure{
    if (!_sure) {
        _sure =  [[UIButton alloc]init];
        [_sure setTitle:@"确认" forState:UIControlStateNormal];
        [_sure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sure.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_sure addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sure;
}

@end
